home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / misc / vb3utils / general.bas next >
Encoding:
BASIC Source File  |  1996-01-08  |  9.6 KB  |  259 lines

  1. Option Explicit
  2.  
  3. Const SWP_NOMOVE = 2
  4. Const SWP_NOSIZE = 1
  5. Const HWND_TOPMOST = -1
  6. Const HWND_NOTOPMOST = -2
  7.  
  8. Const SW_RESTORE = 9
  9. Const SW_SHOWNORMAL = 1
  10. Const SW_SHOWMINIMIZED = 2
  11. Const SW_SHOWMAXIMIZED = 3
  12.  
  13. Global Const WPR_LEFT = &H1
  14. Global Const WPR_TOP = &H2
  15. Global Const WPR_LEFTTOP = WPR_LEFT + WPR_TOP
  16. Global Const WPR_WIDTH = &H4
  17. Global Const WPR_HEIGHT = &H8
  18. Global Const WPR_WIDTHHEIGHT = WPR_WIDTH + WPR_HEIGHT
  19. Global Const WPR_STATE = &H10
  20. Global Const WPR_ALL = WPR_LEFTTOP + WPR_WIDTHHEIGHT + WPR_STATE
  21.  
  22. Global Const SND_SYNC = &H0               ' play synchronously (default)
  23. Global Const SND_ASYNC = &H1              ' play asynchronously
  24. Global Const SND_NODEFAULT = &H2          ' don't use default sound
  25. Global Const SND_MEMORY = &H4             ' lpszSoundName points to a memory file
  26. Global Const SND_LOOP = &H8               ' loop the sound until next sndPlaySound
  27. Global Const SND_NOSTOP = &H10            ' don't stop any currently playing sound
  28.  
  29. Type POINTAPI
  30.     x As Integer
  31.     y As Integer
  32. End Type
  33.  
  34. Type RECT
  35.     left As Integer
  36.     top As Integer
  37.     right As Integer
  38.     bottom As Integer
  39. End Type
  40.  
  41. Type WINDOWPLACEMENT
  42.     length As Integer
  43.     Flags As Integer
  44.     showCmd As Integer
  45.     ptMinPosition As POINTAPI
  46.     ptMaxPosition As POINTAPI
  47.     rcNormalPosition As RECT
  48. End Type
  49.  
  50. Declare Function GAW% Lib "User" Alias "GetActiveWindow" ()
  51. Declare Function SWP% Lib "user" Alias "SetWindowPos" (ByVal h%, ByVal hb%, ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal f%)
  52. Declare Function FW% Lib "user" Alias "FindWindow" (ByVal lpClassName As Any, ByVal lpCaption As Any)
  53. Declare Function SW% Lib "User" Alias "ShowWindow" (ByVal Handle As Integer, ByVal Cmd As Integer)
  54. Declare Function SF% Lib "User" Alias "SetFocus" (ByVal Handle As Integer)
  55.  
  56. Declare Function GPPS% Lib "Kernel" Alias "GetPrivateProfileString" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String)
  57. Declare Function GPS% Lib "Kernel" Alias "GetProfileString" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer)
  58. Declare Function WPPS% Lib "Kernel" Alias "WritePrivateProfileString" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpString As Any, ByVal lplFileName As String)
  59. Declare Function WPS% Lib "Kernel" Alias "WriteProfileString" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpString As Any)
  60.  
  61. Declare Function GWPlc% Lib "User" Alias "GetWindowPlacement" (ByVal hWnd As Integer, lpwndpl As WINDOWPLACEMENT)
  62. Declare Function SWPlc% Lib "User" Alias "SetWindowPlacement" (ByVal hWnd As Integer, lpwndpl As WINDOWPLACEMENT)
  63.  
  64. Declare Function Sound1% Lib "mmsystem" Alias "sndPlaySound" (ByVal lpSoundName As String, ByVal Flags As Integer)
  65. Declare Function Sound0% Lib "mmsystem" Alias "sndPlaySound" (ByVal lpSoundName As Any, ByVal Flags As Integer)
  66.  
  67. ' NOTE: Does not return if duplicate found !!
  68. '
  69. Sub ExitIfAlreadyRunning ()
  70. Dim Title$, Handle%, junk%
  71.     ' Get App title
  72.     Title$ = App.Title
  73.     ' Set it to something unlikely ...
  74.     App.Title = "TestingForDuplicate"
  75.     ' Search for another app with my title
  76.     Handle% = FW(0&, Title$)
  77.     ' Set my App's title back to what it was
  78.     App.Title = Title$
  79.     ' If another copy was found ...
  80.     If Handle% <> 0 Then
  81.     ' Handle% will probably point to App, so we
  82.     '  need to give the App the focus to find it's
  83.     '  Main Window ...
  84.     junk% = SF(Handle%)
  85.     ' ... Get the window ...
  86.     Handle% = GAW()
  87.     ' ... and restore it if possible
  88.     If Handle% <> 0 Then
  89.         junk% = SW(Handle%, SW_RESTORE)
  90.         ' Now exit myself and it's done !
  91.         End
  92.     End If
  93.     End If
  94. End Sub
  95.  
  96. ' Sets a window to be floating or non-floating
  97. ' Pass form, true for floating, false for not
  98. ' returns true if successful, otherwise false
  99. Function FormFloat (FloatForm As Form, Floating%) As Integer
  100. Const Flags = SWP_NOMOVE Or SWP_NOSIZE
  101. Dim success%
  102.     If Floating Then
  103.     success% = SWP(FloatForm.hWnd, HWND_TOPMOST, 0, 0, 0, 0, Flags)
  104.     Else
  105.     success% = SWP(FloatForm.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, Flags)
  106.     End If
  107.     ' success% <> 0 When Successful
  108.     FormFloat = success%
  109. End Function
  110.  
  111. Function GetPrivateProfileString (ByVal ApplicationName As String, KeyName As String, default As String, Size As Integer, FileName As String) As String
  112. Dim TmpStr As String, retcode As Integer
  113.     GetPrivateProfileString = ""
  114.     If ApplicationName = "" Then
  115.     ApplicationName = App.Title
  116.     End If
  117.     TmpStr = String$(Size, 0)
  118.     retcode = GPPS(ApplicationName, KeyName, default, TmpStr, Size, FileName)
  119.     If retcode > 0 Then
  120.     GetPrivateProfileString = Left$(TmpStr, retcode)
  121.     End If
  122. End Function
  123.  
  124. Function GetProfileString (ByVal ApplicationName As String, KeyName As String, default As String, Size As Integer) As String
  125. Dim TmpStr As String, retcode As Integer
  126.     GetProfileString = ""
  127.     If ApplicationName = "" Then
  128.     ApplicationName = App.Title
  129.     End If
  130.     TmpStr = String$(Size, 0)
  131.     retcode = GPS(ApplicationName, KeyName, default, TmpStr, Size)
  132.     If retcode > 0 Then
  133.     GetProfileString = Left$(TmpStr, retcode)
  134.     End If
  135. End Function
  136.  
  137. ' Plays sound defined in WIN.INI [Sounds]
  138. '
  139. Function PlaySound (SoundName As String)
  140.     PlaySound = Sound1%(SoundName, SND_ASYNC)
  141. End Function
  142.  
  143. ' Plays sound defined in WIN.INI [Sounds]
  144. '  Flags defined in [Declarations]
  145. '
  146. Function PlaySoundExt (SoundName As String, Flags As Integer)
  147.     PlaySoundExt = Sound1%(SoundName, Flags)
  148. End Function
  149.  
  150. ' Restores a form's position from WIN.INI under heading AppName
  151. ' If AppName = "" use App.Title
  152. ' Flags defined in [Declarations]. Can be used to only restore
  153. ' parts of the position. i.e. only restore Left & Top without
  154. ' restoring Width, Height, and State
  155. '
  156. ' This is usually called after form is loaded, but before it is
  157. ' 'Show'n.
  158. '
  159. Function RestoreWindowPos (AppForm As Form, AppName As String, Flags As Integer) As Integer
  160. Dim WindowPos As WINDOWPLACEMENT
  161. Dim retcode As Integer
  162. Dim PosStr As String, Comma As Integer
  163.     retcode = False
  164.     PosStr = GetProfileString(AppName, "WindowPosition", "", 255)
  165.     If PosStr <> "" Then
  166.     WindowPos.length = 22
  167.     WindowPos.Flags = 0
  168.     If Left$(PosStr, 1) = "[" And Right$(PosStr, 1) = "]" Then
  169.         PosStr = Mid$(PosStr, 2, Len(PosStr) - 2) + ","
  170.         Comma = InStr(PosStr, ",")
  171.         If Comma > 1 Then
  172.         If (Flags And WPR_LEFT) Then
  173.             WindowPos.rcNormalPosition.left = Val(Left$(PosStr, Comma - 1))
  174.         Else
  175.             WindowPos.rcNormalPosition.left = Int(AppForm.Left / screen.TwipsPerPixelX)
  176.         End If
  177.         PosStr = Mid$(PosStr, Comma + 1)
  178.         Comma = InStr(PosStr, ",")
  179.         If Comma > 1 Then
  180.             If (Flags And WPR_TOP) Then
  181.             WindowPos.rcNormalPosition.top = Val(Left$(PosStr, Comma - 1))
  182.             Else
  183.             WindowPos.rcNormalPosition.top = Int(AppForm.Top / screen.TwipsPerPixelY)
  184.             End If
  185.             PosStr = Mid$(PosStr, Comma + 1)
  186.             Comma = InStr(PosStr, ",")
  187.             If Comma > 1 Then
  188.             If (Flags And WPR_WIDTH) Then
  189.                 WindowPos.rcNormalPosition.right = WindowPos.rcNormalPosition.left + Val(Left$(PosStr, Comma - 1))
  190.             Else
  191.                 WindowPos.rcNormalPosition.right = WindowPos.rcNormalPosition.left + Int(AppForm.Width / screen.TwipsPerPixelX)
  192.             End If
  193.             PosStr = Mid$(PosStr, Comma + 1)
  194.             Comma = InStr(PosStr, ",")
  195.             If Comma > 1 Then
  196.                 If (Flags And WPR_HEIGHT) Then
  197.                 WindowPos.rcNormalPosition.bottom = WindowPos.rcNormalPosition.top + Val(Left$(PosStr, Comma - 1))
  198.                 Else
  199.                 WindowPos.rcNormalPosition.bottom = WindowPos.rcNormalPosition.top + Int(AppForm.Height / screen.TwipsPerPixelY)
  200.                 End If
  201.                 PosStr = Mid$(PosStr, Comma + 1)
  202.                 Comma = InStr(PosStr, ",")
  203.                 If Comma > 1 Then
  204.                 If (Flags And WPR_STATE) Then
  205.                     WindowPos.showCmd = Val(Left$(PosStr, Comma - 1))
  206.                 Else
  207.                     WindowPos.showCmd = IIf(AppForm.WindowState = 1, SW_SHOWMINIMIZED, IIf(AppForm.WindowState = 2, SW_SHOWMAXIMIZED, SW_SHOWNORMAL))
  208.                 End If
  209.                 retcode = True
  210.                 End If
  211.             End If
  212.             End If
  213.         End If
  214.         End If
  215.         If retcode Then
  216.         retcode = SWPlc(AppForm.hWnd, WindowPos)
  217.         End If
  218.     End If
  219.     End If
  220.     RestoreWindowPos = retcode
  221. End Function
  222.  
  223. ' Saves a form's position in WIN.INI under heading AppName
  224. ' If AppName = "" use App.Title
  225. '
  226. Function SaveWindowPos (AppForm As Form, AppName As String) As Integer
  227. Dim WindowPos As WINDOWPLACEMENT, retcode As Integer
  228. Dim PosStr As String
  229.     WindowPos.length = 22
  230.     retcode = GWPlc(AppForm.hWnd, WindowPos)
  231.     If retcode Then
  232.     PosStr = "[" + CStr(WindowPos.rcNormalPosition.left) + "," + CStr(WindowPos.rcNormalPosition.top) + "," + CStr(WindowPos.rcNormalPosition.right) + "," + CStr(WindowPos.rcNormalPosition.bottom) + "," + CStr(WindowPos.showCmd) + "]"
  233.     retcode = WriteProfileString(AppName, "WindowPosition", PosStr)
  234.     End If
  235.     SaveWindowPos = retcode
  236. End Function
  237.  
  238. ' Stops any ASYNC sound being played.
  239. '
  240. Sub StopSound ()
  241. Dim junk As Integer
  242.     junk = Sound0%(0&, 0)
  243. End Sub
  244.  
  245. Function WritePrivateProfileString (ByVal ApplicationName As String, KeyName As String, Value As String, FileName As String) As Integer
  246.     If ApplicationName = "" Then
  247.     ApplicationName = App.Title
  248.     End If
  249.     WritePrivateProfileString = WPPS(ApplicationName, KeyName, Value, FileName)
  250. End Function
  251.  
  252. Function WriteProfileString (ByVal ApplicationName As String, KeyName As String, Value As String) As Integer
  253.     If ApplicationName = "" Then
  254.     ApplicationName = App.Title
  255.     End If
  256.     WriteProfileString = WPS(ApplicationName, KeyName, Value)
  257. End Function
  258.  
  259.